﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CloudApiServer.Services
{
    /// <summary>
    /// 事前事中分析服务
    /// </summary>
    public class RiskConService
    {
        /// <summary>
        /// 3101 明细审核事前分析
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="CountrollerName">来源控制器名称</param>
        /// <returns>出参</returns>
        /// <exception cref="Exception"></exception>
        public async Task<OutputriskConService> BeforeAnalyze(FsiRequestHeader<InputRiskConServiceresult> fsiRequest, string CountrollerName)
        {
            string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
            string gend = fsiRequest.Input.data.patient_dtos.gend;//性别
            string patn_id = fsiRequest.Input.data.patient_dtos.patn_id;//人员编号
            string curr_mdtrt_id = fsiRequest.Input.data.patient_dtos.curr_mdtrt_id;//就诊id
            List<DatariskConServiceresult> dataList = new List<DatariskConServiceresult>();
            List<ListRiskConServiceresultFsi_order_dtos> fsi_order_dtos_List;
            if (CountrollerName== "FsiApiZheJiangController" && fsiadmvs == "61")
            {
                fsi_order_dtos_List = fsiRequest.Input.data.patient_dtos.encounter_dtos.order_dtos;
            }
            else
            {
                fsi_order_dtos_List = fsiRequest.Input.data.patient_dtos.fsi_encounter_dtos.fsi_order_dtos;
            }

            string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
            using (var conn = new MySqlConnection(connectionString))
            {
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                long dtunix = PublicMethod.ConvertToUnixTimestampMillis(dateTime);
                await conn.OpenAsync();
                using (MySqlCommand cmd = conn.CreateCommand())
                {
                    foreach (var item in fsi_order_dtos_List)
                    {
                        cmd.CommandText = "select  * from  fsi_riskconserviceproduct where med_list_codg = @med_list_codg ";
                        cmd.Parameters.AddWithValue("@med_list_codg", item.hilist_code);
                        using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                        {
                            DataTable dt = new DataTable();
                            dt.Load(reader);
                            if (dt.Rows.Count >= 1)
                            {
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    List<Judge_result_detail_dtosItem> JudgeresultdetaildtosItem = new List<Judge_result_detail_dtosItem>();
                                    Judge_result_detail_dtosItem Judge_result_detail_dtosItem = new Judge_result_detail_dtosItem
                                    {
                                        vola_item_type = "1",
                                        mdtrt_id = curr_mdtrt_id,
                                        jrd_id = Guid.NewGuid().ToString(),
                                        patn_id = patn_id,
                                        rx_id = item.rx_id,
                                        vola_amt = item.sumamt
                                    };
                                    JudgeresultdetaildtosItem.Add(Judge_result_detail_dtosItem);
                                    DatariskConServiceresult riskConServiceresultInfo = new DatariskConServiceresult
                                    {
                                        judge_result_detail_dtos = JudgeresultdetaildtosItem,
                                        rule_id = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}",
                                        vola_evid = dt.Rows[i]["vola_evid"].ToString(),
                                        rule_name = dt.Rows[i]["rule_name"].ToString(),
                                        mdtrt_id = curr_mdtrt_id,
                                        vola_amt_stas = "1",
                                        jr_id = Guid.NewGuid().ToString(),
                                        patn_id = patn_id,
                                        vola_amt = 1,
                                        sev_deg = dt.Rows[i]["sev_deg"].ToString(),
                                        vola_cont = dt.Rows[i]["vola_cont"].ToString(),
                                        vola_bhvr_type = dt.Rows[i]["vola_bhvr_type"].ToString()
                                    };
                                    dataList.Add(riskConServiceresultInfo);
                                }

                            }
                            dt.Dispose();
                            reader.Close();
                            reader.Dispose();
                        }
                        cmd.Parameters.Clear();
                    }
                }
                var output = new OutputriskConService
                {
                    result = dataList
                };
                return output;
            }
        }
        /// <summary>
        /// 3102 明细审核事中分析
        /// </summary>
        /// <param name="fsiRequest">入参</param>
        /// <param name="CountrollerName">来源控制器名称</param>
        /// <returns>出参</returns>
        /// <exception cref="Exception"></exception>
        public async Task<OutputriskConService> CourseAnalyze(FsiRequestHeader<InputRiskConServiceresult> fsiRequest, string CountrollerName)
        {
            string fsiadmvs = fsiRequest.mdtrtarea_admvs.Substring(0, 2);
            string gend = fsiRequest.Input.data.patient_dtos.gend;//性别
            string patn_id = fsiRequest.Input.data.patient_dtos.patn_id;//人员编号
            string curr_mdtrt_id = fsiRequest.Input.data.patient_dtos.curr_mdtrt_id;//就诊id
            List<DatariskConServiceresult> dataList = new List<DatariskConServiceresult>();
            List<ListRiskConServiceresultFsi_order_dtos> fsi_order_dtos_List;
            if (CountrollerName == "FsiApiZheJiangController" && fsiadmvs == "61")
            {
                fsi_order_dtos_List = fsiRequest.Input.data.patient_dtos.encounter_dtos.order_dtos;
            }
            else
            {
                fsi_order_dtos_List = fsiRequest.Input.data.patient_dtos.fsi_encounter_dtos.fsi_order_dtos;
            }

            string connectionString = $"Database={ApiDatainfo.mysqlDatabase};Data Source={ApiDatainfo.mysqlUrl};Port={ApiDatainfo.mysqlPost};UserId={ApiDatainfo.mysqlUser};Password={ApiDatainfo.mysqlPwd};Charset=utf8mb4;TreatTinyAsBoolean=false;Allow User Variables=True";
            using (var conn = new MySqlConnection(connectionString))
            {
                Random rnd = new Random();
                DateTime dateTime = DateTime.Now;
                long dtunix = PublicMethod.ConvertToUnixTimestampMillis(dateTime);
                await conn.OpenAsync();
                using (MySqlCommand cmd = conn.CreateCommand())
                {
                    foreach (var item in fsi_order_dtos_List)
                    {
                        cmd.CommandText = "select  * from  fsi_riskconserviceproduct where med_list_codg = @med_list_codg ";
                        cmd.Parameters.AddWithValue("@med_list_codg", item.hilist_code);
                        using (MySqlDataReader reader = (MySqlDataReader)await cmd.ExecuteReaderAsync())
                        {
                            DataTable dt = new DataTable();
                            dt.Load(reader);
                            if (dt.Rows.Count >= 1)
                            {
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    List<Judge_result_detail_dtosItem> JudgeresultdetaildtosItem = new List<Judge_result_detail_dtosItem>();
                                    Judge_result_detail_dtosItem Judge_result_detail_dtosItem = new Judge_result_detail_dtosItem
                                    {
                                        vola_item_type = "1",
                                        mdtrt_id = curr_mdtrt_id,
                                        jrd_id = Guid.NewGuid().ToString(),
                                        patn_id = patn_id,
                                        rx_id = item.rx_id,
                                        vola_amt = item.sumamt
                                    };
                                    JudgeresultdetaildtosItem.Add(Judge_result_detail_dtosItem);
                                    DatariskConServiceresult riskConServiceresultInfo = new DatariskConServiceresult
                                    {
                                        judge_result_detail_dtos = JudgeresultdetaildtosItem,
                                        rule_id = $"{fsiadmvs}0000{dateTime.ToString("yyyyMMddHHmmssfff")}{rnd.Next(1000000, 10000000).ToString()}",
                                        vola_evid = dt.Rows[i]["vola_evid"].ToString(),
                                        rule_name = dt.Rows[i]["rule_name"].ToString(),
                                        mdtrt_id = curr_mdtrt_id,
                                        vola_amt_stas = "1",
                                        jr_id = Guid.NewGuid().ToString(),
                                        patn_id = patn_id,
                                        vola_amt = 1,
                                        sev_deg = dt.Rows[i]["sev_deg"].ToString(),
                                        vola_cont = dt.Rows[i]["vola_cont"].ToString(),
                                        vola_bhvr_type = dt.Rows[i]["vola_bhvr_type"].ToString()
                                    };
                                    dataList.Add(riskConServiceresultInfo);
                                }

                            }
                            dt.Dispose();
                            reader.Close();
                            reader.Dispose();
                        }
                        cmd.Parameters.Clear();
                    }
                }
                var output = new OutputriskConService
                {
                    result = dataList
                };
                return output;
            }
        }

    }
}
